package com.dm;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/test")
@CrossOrigin
public class TestController {

    public static List<User> userList = new ArrayList<>();

    @GetMapping("/get")
    public List<User> get() {
        return userList;
    }

    @PostMapping("/set")
    public List<User> set(@RequestBody Map<String, String> map) {
        if (map.get("code").equals("M")) {
            User user = new User();
            user.setCode(map.get("code"));
            user.setName(map.get("name") == null ? "加米饭" : map.get("name"));
            user.setScore(999999);
            userList.add(user);
        } else {
            for (User user : userList) {
                if (user.getName().equals(map.get("name"))) {
                    return userList;
                }
            }
            User user = new User();
            user.setName(map.get("name"));
            user.setCode(map.get("code"));
            user.setScore(getNumber());
            userList.add(user);
        }
        userList = userList.stream().sorted(Comparator.comparingInt(User::getScore)).collect(Collectors.toList());
        return userList;
    }

    public static Integer getNumber() {
        if (userList.size() > 99) {
            return -1;
        }
        Random random = new Random();
        int randomNumber = random.nextInt(101);
        for (User user : userList) {
            if (user.getScore() == randomNumber) {
                randomNumber = getNumber();
            }
        }
        return randomNumber;
    }

    /**
     * 解析抖音视频
     *
     * @param url 抖音视频的URL链接
     * @return
     */
    @GetMapping("/analysisVideo")
    public Object analysisVideo(String url) {
        String get = HttpUtil.get("http://i.rcuts.com/update/247");

        int https = url.indexOf("https");
        String substring = url.substring(https);
        Map<String, Object> params = new HashMap<>();
        params.put("url", "mailto:f@o.cn" + substring);
        params.put("token", get);
        params.put("clipboard", url);
        String post = HttpUtil.post("http://api.rcuts.com/Video/DouYin.php", params);
        JSONObject jsonObject = JSONUtil.parseObj(post);
        if (jsonObject.get("code").toString().equals("0")) {
            JSONArray list = JSONUtil.parseArray(jsonObject.get("video_url"));
            Object o = list.get(0);
            Map<String, Object> r = new HashMap<>();
            r.put("data", o);
            return r;
        }
        return null;
    }

    @GetMapping("/lookVideo")
    public Object lookVideo() {
        JSONObject resObj = new JSONObject();
        try {
            String post = HttpRequest.post("https://www.free-api.com/urltask?type=json&fzsid=644")
                    .header("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8") // 设置请求头
                    .header("cookie", "agree=1; Hm_lvt_6188d53492b3951c5aa16a77f0b0e858=1727577948,1728712086; HMACCOUNT=E4ABF360FD81FD2C; __gads=ID=26e181f4e04e54f0:T=1727577949:RT=1728712088:S=ALNI_MbKeoFJ33V2kF-9j3u0jlbliAqpLA; __gpi=UID=00000f22795164cf:T=1727577949:RT=1728712088:S=ALNI_MZeywzsd-vkyZ0ONaTo-ySVeQfFkQ; __eoi=ID=a114548ba46962f6:T=1727577949:RT=1728712088:S=AA-AfjbAqHcls4BrGodQ6kLu-W56; token=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJodHRwczpcL1wvd3d3LmZyZWUtYXBpLmNvbSIsImF1ZCI6Imh0dHBzOlwvXC93d3cuZnJlZS1hcGkuY29tIiwiaWF0IjoxNzI4NzEyMjAyLCJleHAiOjE3Mjg3OTg2MDIsImRhdGEiOnsidXNlcm5hbWUiOiI3OTM5MTE5MTlAcXEuY29tIiwidWlkIjoiNzkzOTExOTE5QHFxLmNvbSIsInR5cGUiOjEsImltZ3NyYyI6Imh0dHBzOlwvXC9xMS5xbG9nby5jblwvZz9iPXFxJm5rPTc5MzkxMTkxOSZzPTEwMCZ0PTE3Mjc1NzgyNDkifX0.Ydemrs-XlKQCUJheYtMoJA2VGqH3DZ3NQHOKqEhB2Lk; Hm_lpvt_6188d53492b3951c5aa16a77f0b0e858=1728712277; XSRF-TOKEN=eyJpdiI6IkdWZmJEeHBlY1p6MWpDZW9LYU11UFE9PSIsInZhbHVlIjoiSXNoUzIwWXhwU1VcL1wvbWFTZDZQMGRmaW5ad1p6bTRmVlhpcTAxMTJoYWxJdDliTDZLdUVtVzg1XC92UlFxOXJiaCIsIm1hYyI6IjYzYmFiYWVmMDk2NWNjZmI0ZDM1YWU3ODQ4NDg0NWVhZTFmODJjNGY1ZDBjN2UxNjc3ZWI3ODJiMTFlYTZmNDAifQ%3D%3D; laravel_session=eyJpdiI6InF3UUpsVjN6ajdjXC9KN2hHVk1CMTh3PT0iLCJ2YWx1ZSI6Ik5EUXI0bkJHamVCWEhYaDFkOEFrclZCSUZhMkE1bGxWVEJXXC9OMkZ2djZ5TTJnOHdJWmh5S1gxazdZZmRMSzR1IiwibWFjIjoiMzAxMGMxZWZjOWJkMmZmNzAwYzNlMTQ5M2U0OGYwZTE5NWFhYzYzMjc3Yjc0MGYyZTVlZjM2ZDBiYTExMTFlYSJ9") // 添加其他头部
                    .header("x-csrf-token", "wfuqpRi6FlljvwfAnSYtxsi2Y408Flufc1xjCMzw")
                    .execute()
                    .body();
            System.out.println("随机视频响应结果：");
            System.out.println(post);
            JSONObject res = JSONUtil.parseObj(post);
            JSONObject jsonObject1 = res.getJSONObject("msg");
            Integer code = jsonObject1.getInt("code");
            if (code == 200) {
                resObj.set("code", 200);
                resObj.set("data", jsonObject1.getStr("video"));
                return resObj;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        resObj.set("code", 500);
        return resObj;
    }

    class User {
        private String name;
        private String code;
        private Integer score;
        private Integer rice;

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getCode() {
            return code;
        }

        public void setCode(String code) {
            this.code = code;
        }

        public Integer getScore() {
            return score;
        }

        public void setScore(Integer score) {
            this.score = score;
        }

        public Integer getRice() {
            return rice;
        }

        public void setRice(Integer rice) {
            this.rice = rice;
        }
    }

    public static void main(String[] args) {
        String url = "4.66 复制打开抖音，看看【三三驾到的作品】摩展会# # 2024重庆摩博会  https://v.douyin.com/ikY5Uuxa/ A@T.Lw TyT:/ 05/02 ";
        String get = HttpUtil.get("http://i.rcuts.com/update/247");

        int https = url.indexOf("https");
        String substring = url.substring(https);
        Map<String, Object> params = new HashMap<>();
        params.put("url", "mailto:f@o.cn" + substring);
        params.put("token", get);
        params.put("clipboard", url);
        String post = HttpUtil.post("http://api.rcuts.com/Video/DouYin.php", params);
        JSONObject jsonObject = JSONUtil.parseObj(post);
        if (jsonObject.get("code").toString().equals("0")) {
            JSONArray list = JSONUtil.parseArray(jsonObject.get("video_url"));
            Object o = list.get(0);
            Map<String, Object> r = new HashMap<>();
            r.put("data", o);
        }
    }
}

